<template>
  <div class="login">
    <Vcode :show="isShow" :imgs="imgs" @success="success" @close="close" />
    <vue-particles
      color="#fff"
      :particleOpacity="0.7"
      :particlesNumber="60"
      shapeType="circle"
      :particleSize="4"
      linesColor="#fff"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="8"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
      class="lizi"
    ></vue-particles>
    <el-form class="login-container" label-position="left" label-width="0px">
      <h3 class="login_title">工单系统登录</h3>
      <el-form-item>
        <el-input
          type="text"
          v-model="user.username"
          auto-complete="off"
          @keyup.enter.native="doLogin"
          placeholder="账号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          type="password"
          v-model="user.password"
          auto-complete="off"
          @keyup.enter.native="doLogin"
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          style="width: 100%;background: #505458;border: none"
          @click="isShow=true"
        >登录</el-button>
      </el-form-item>
      <el-link target="_blank" @click="dialogFormVisible = true" style="color:white">注册</el-link>
    </el-form>

    <el-dialog title="注册" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
          <el-input style="position:fixed;bottom:-9999px;"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input style="position:fixed; top:-9999px;" type="password"></el-input>
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="ruleForm.age"></el-input>
        </el-form-item>

        <el-form-item label="用户类型" prop="userType">
          <el-select v-model="ruleForm.roles.id" placeholder="请选择用户类型">
            <el-option label="工人" :value="2"></el-option>
            <el-option label="客户" :value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户性别" prop="sex">
          <el-radio-group v-model="ruleForm.sex" size="medium">
            <el-radio border label="男"></el-radio>
            <el-radio border label="女"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="电话" prop="tel">
          <el-input v-model="ruleForm.tel" @keyup.enter.native="save('ruleForm')"></el-input>
        </el-form-item>

          <el-form-item label="地址" prop="addr">
          <el-input v-model="ruleForm.addr" @keyup.enter.native="save('ruleForm')"></el-input>
        </el-form-item>

        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="save('ruleForm')">确 定</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { mapMutations } from "vuex";
import axios from "axios";
import Vcode from "vue-puzzle-vcode";
import Img1 from "../../assets/img1.jpg";
import Img2 from "../../assets/img2.jpg";
import Img3 from "../../assets/img3.jpg";
import Img4 from "../../assets/img4.jpg";

export default {
  components:{
    Vcode       
  },
  //单页面中不支持前面的data:{}方式
  data() {
    return {
      user: {
        username: "admin",
        password: "123456",
      },
      imgs: [Img1, Img2, Img3, Img4],
      isShow: false,

      ruleForm: {
        username: "",
        password: "",
        age: "",
        sex: "男",
        tel: "",
        roles: { id: 2 },
        userstate: 0,
        addr:""
      },

      rules: {
        username: [{ required: true, message: "请输入名字", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      dialogFormVisible: false,
    };
  },

  methods: {
    ...mapMutations(["changeLogin"]),
    ...mapMutations(["changeLogin2"]),
  
 success() {
      this.doLogin(); // 通过验证后，需要手动隐藏模态框
      this.isShow = false;
    },
    close() {
      this.isShow = false;
    },

    doLogin() {
      let that = this;
      this.$axios({
        method: "post",
        url: "/user/login",
        data: {
          username: that.user.username,
          password: that.user.password,
        },
      })
        .then(function (res) {
          if (res.data.code === 200) {
            // 将token放入vuex
            that.changeLogin({ Authorization: res.data.result.token });
            window.localStorage.setItem("roleId", res.data.result.roleId);
            // window.localStorage.setItem('Authorization', res.data.result.token)
            that.changeLogin2({ username: res.data.result.username });
          
            that.$router.push({
              path: "/Layout",
            });
            that.$message("登录成功");
          } else {
            console.log(res);
            that.$message("密码或用户名错误");
          }
        })
        .catch(function (err) {
          console.log("err", err);
          that.$message("密码或用户名错误");
        });
    },
    save(formName) {
      let that = this;
      that.$refs[formName].validate((valid) => {
        if (valid) {
          axios({
            method: "post",
            data: that.ruleForm,
            url: "/user/register",
          })
            .then(function (res) {
              console.log(res);
              if (res.data.code === 200) {
                that.dialogFormVisible = false;
                that.$refs[formName].resetFields();
                that.$message.success("注册成功");
                that.$router.push({
                  name: "login",
                });
              } else {
                that.$message.success("注册失败");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    },
  },
};
</script>
<style>
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  margin-top: -700px;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: white;
}
.login {
  width: 100%;
  height: 968px;
  background-image: url("../../assets/login_bg.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}
</style>
